package net.bithappens.hnqe.scratchpad;

import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFormatter;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.tdb.TDBFactory;



public class JenaClient {

	public static void main(String[] args) throws Exception {
		
//		Model model = TDBFactory.createModel("C:\\Users\\armin\\workspace\\HyperNetworkQueryEngine\\RDFDB");
//		Model model = TDBFactory.createModel("J:\\RDFDB\\sose");
		Model model = TDBFactory.createModel("/home/armin/RDFDB");
		
		showEveryThing(model);
//		testQuery(model);
		
//		createResources(model);
//		executeQuery(model);
		

	}

	
	
	private static void testQuery(Model model) {
		
		String queryString =
				"PREFIX meta: <hnqe://meta/> PREFIX prop: <hnqe://property/> PREFIX rel: <hnqe://relation/> PREFIX value: <hnqe://value/value> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> PREFIX afn: <http://jena.hpl.hp.com/ARQ/function#> " +
				"SELECT DISTINCT ?N1 ?R1 ?N0 ?R0 ?N2 WHERE {{?N0 prop:type ?N0_type . FILTER( ?N0_type = \"TypeA\"^^xsd:string) . ?N1 prop:type ?N1_type . FILTER( ?N1_type = \"TypeB\"^^xsd:string) . " +
				"?N0 meta:typeId \"Service\"^^xsd:string . ?R0 ?rel0 ?N0 . FILTER(afn:namespace(?rel0) = \"hnqe://relation/\"^^xsd:string) . ?N1 meta:typeId \"SoftwareBasedService\"^^xsd:string . ?R0 ?rel1 ?N1 . FILTER(afn:namespace(?rel1) = \"hnqe://relation/\"^^xsd:string) . " +
				"?N0 meta:typeId \"Service\"^^xsd:string . ?R1 rel:entry-0 ?N0 . ?N2 meta:typeId \"HumanProvidedService\"^^xsd:string . ?R1 rel:entry-1 ?N2 . ?R1 prop:type ?R1_type . FILTER( ?R1_type = \"TypeA\"^^xsd:string) . } union {" +
				"?N0 prop:type ?N0_type . FILTER( ?N0_type = \"TypeA\"^^xsd:string) . ?N1 prop:type ?N1_type . FILTER( ?N1_type = \"TypeB\"^^xsd:string) . ?N0 meta:typeId \"Service\"^^xsd:string . ?R0 ?rel0 ?N0 . " +
				"FILTER(afn:namespace(?rel0) = \"hnqe://relation/\"^^xsd:string) . ?N1 meta:typeId \"SoftwareBasedService\"^^xsd:string . ?R0 ?rel1 ?N1 . FILTER(afn:namespace(?rel1) = \"hnqe://relation/\"^^xsd:string) . " +
				"?N0 meta:typeId \"Service\"^^xsd:string . ?R1 rel:entry-0 ?N0 . ?N2 meta:typeId \"HumanProvidedService\"^^xsd:string . ?R1 rel:entry-1 ?N2 . ?R1 prop:type ?R1_type . FILTER( ?R1_type = \"TypeC\"^^xsd:string) . }}";					
		
		Query query = QueryFactory.create(queryString);
		// Execute the query and obtain results
		QueryExecution qe = QueryExecutionFactory.create(query, model);
		ResultSet results = qe.execSelect();
		
		
		ResultSetFormatter.out(System.out, results, query);
		// Important - free up resources used running the query
		qe.close();
	}
		

//	private static void testQuery(Model model) {
//		
//		String queryString =
//				"PREFIX meta: <hnqe://meta/> " + 
//				"PREFIX prop: <hnqe://property/> " + 
//				"PREFIX rel: <hnqe://relation/>  " +
//				"PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> " + 
//				"PREFIX afn: <http://jena.hpl.hp.com/ARQ/function#>  " +
//				"SELECT DISTINCT ?J " +
//				"WHERE { " +
//					"{" +
//					"?J prop:name ?lj . " +
//					"{{FILTER regex(?lj, \"John\"^^xsd:string, \"i\")} union {FILTER regex(?lj, \"Alice\"^^xsd:string, \"i\")}}  " +
//					"}" +
//				"} ";
//						
//		Query query = QueryFactory.create(queryString);
//		// Execute the query and obtain results
//		QueryExecution qe = QueryExecutionFactory.create(query, model);
//		ResultSet results = qe.execSelect();
//		
//		
//		ResultSetFormatter.out(System.out, results, query);
//		// Important - free up resources used running the query
//		qe.close();
//	}
//	

	private static void createResources(Model model) throws Exception {
		
		model.begin();
		model.removeAll();
		
		Resource bob = model.createResource("http://test.com/bob2");
		bob.addLiteral(model.createProperty("name"), "bob");

		
		Resource res = model.getResource("http://test.com/bob1");
		Resource res2 = model.getResource("http://test.com/bob2");
		
		
		if(res == null){
			System.out.println("res is null");
		} else {
			System.out.println(res);
			System.out.println(res.isAnon());
			System.out.println(res.isLiteral());
			System.out.println(res.isResource());
			System.out.println(res.isURIResource());
			System.out.println(model.containsResource(res));
			
			System.out.println(model.containsResource(res2));
		}
		
		model.commit();
		
				
		String queryString =
				"SELECT ?X ?prop ?Y " +
				"WHERE {" +
				"?X ?prop ?Y. " +
				"}";
		
				
		Query query = QueryFactory.create(queryString);
		// Execute the query and obtain results
		QueryExecution qe = QueryExecutionFactory.create(query, model);
		ResultSet results = qe.execSelect();	
		ResultSetFormatter.out(System.out, results, query);
		// Important - free up resources used running the query
		qe.close();
	
		

	}

	
	private static void executeQuery(Model model){
		
		// Create a new query
//		String queryString =
//			"PREFIX meta: <" + NamingConstants.NAMESPACE_META + "> PREFIX prop: <hnqe://property/> PREFIX rel: <hnqe://relation/> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> " +
//			"SELECT DISTINCT ?X ?prop ?Y " +
//			"WHERE { " +
//			"?X ?prop ?Y . " +
//			"?X meta:leaseTimeStamp ?date . " +
//			"FILTER (xsd:dateTime(?date) <= \"2012-01-22T17:26:53.837Z\"^^xsd:dateTime)}";
		
		
		
		
		String queryString =
				"PREFIX meta: <hnqe://meta/> " + 
				"PREFIX prop: <hnqe://property/> " + 
				"PREFIX rel: <hnqe://relation/>  " +
				"PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> " + 
				"PREFIX afn: <http://jena.hpl.hp.com/ARQ/function#>  " +
				"SELECT DISTINCT ?yname ?xname ?Y ?X  " +
				"WHERE { " +
				"{ " +
				"?X prop:name ?xname . " + 
				"FILTER regex(?xname,\"Alice\", \"i\" ) . " + 
				"?relInst_0 meta:instance \"soaf\"^^xsd:string . " + 
				"?relInst_0 meta:relSignature \"provideService(N,N)\"^^xsd:string . " + 
				"?relInst_0 rel:entry-0 ?X .  " +
				"?relInst_0 rel:entry-1 ?Y .  " +
				"?Y prop:name ?yname .  " +
				"FILTER regex(?yname,\"stockNews\", \"i\" ) . " + 
				"} " +
				"{{?Y meta:instance \"soaf\"^^xsd:string} union {?X meta:instance \"soaf\"^^xsd:string}} " +
				"} ";
				
		Query query = QueryFactory.create(queryString);
		// Execute the query and obtain results
		QueryExecution qe = QueryExecutionFactory.create(query, model);
		ResultSet results = qe.execSelect();	
		ResultSetFormatter.out(System.out, results, query);
		// Important - free up resources used running the query
		qe.close();
		
//		
//		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S'Z'");
//		String valueString = sdf.format(new Date());
//		
//		queryString =
//			"PREFIX meta: <" + NamingConstants.NAMESPACE_META + "> \n" +
//			"PREFIX xsd: <" + NamingConstants.NAMESPACE_XSD + "> \n" +
//			"SELECT ?X ?prop ?Y " + 
//			"WHERE { " + 
//				"?X ?prop ?Y . " + 
//				"?X meta:"+NamingConstants.LEASE+" ?date . " + 
//				"FILTER (xsd:dateTime(?date) <= \"" + valueString + "\"^^xsd:dateTime)" + 
//			"}";
//
//
//		query = QueryFactory.create(queryString);
//		// Execute the query and obtain results
//		qe = QueryExecutionFactory.create(query, model);
//		results = qe.execSelect();	
//		ResultSetFormatter.out(System.out, results, query);
//		// Important - free up resources used running the query
//		qe.close();
		
	}
	
	
	
	private static void showEveryThing(Model model) {
		
		String queryString =
				"SELECT ?X ?prop ?Y " +
				"WHERE {" +
				"?X ?prop ?Y. " +
				"}";
						
		Query query = QueryFactory.create(queryString);
		// Execute the query and obtain results
		QueryExecution qe = QueryExecutionFactory.create(query, model);
		ResultSet results = qe.execSelect();
		
//		
//		while(results.hasNext()){
//			
//			QuerySolution sol = results.nextSolution();
//			
//			Iterator<String> variables = sol.varNames();
//			
//			try {
//				
//				while(variables.hasNext()){
//				
//					String variableName = variables.next();
//					
//					RDFNode rdfNode = sol.get(variableName);
//					
//					if(rdfNode.isResource()){
//						System.out.print("|"+variableName+":"+rdfNode.asResource().getURI());
//					}
//					
//					if(rdfNode.isLiteral()){
//						System.out.print("|"+variableName+":"+rdfNode.asLiteral().getValue());
//					}
//	
//				}
//				System.out.println();
//			
//			} catch (Exception e) {
//
//			}
//		}
//		
		
		ResultSetFormatter.out(System.out, results, query);
		// Important - free up resources used running the query
		qe.close();
	}

}
